package com.itheima.imclient100.presenter.impl;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.itheima.imclient100.db.DBUtils;
import com.itheima.imclient100.presenter.AddFriendPresenter;
import com.itheima.imclient100.utils.ThreadUtils;
import com.itheima.imclient100.view.AddFriendView;

import java.util.List;

/**
 * Created by fullcircle on 2017/7/3.
 */

public class AddFriendPresenterImpl implements AddFriendPresenter {
    private AddFriendView addFriendView;

    public AddFriendPresenterImpl(AddFriendView addFriendView) {
        this.addFriendView = addFriendView;
    }

    @Override
    public void searchFriend(String usernamePrefix) {
        final String currentUser = EMClient.getInstance().getCurrentUser();
        //通过leancloud进行好友查询  AVUser leancloud 封装好的对象 对应leancloud服务端_User 这张表中的数据
        AVQuery<AVUser> query = new AVQuery<>("_User");//创建AVQuery 最后一个参数 表名字
        //给查询设置条件  以传入的参数作用用户名的开始
        query.whereStartsWith("username",usernamePrefix)
                .whereNotEqualTo("username",currentUser)// 把当前用户排除掉
                .findInBackground(new FindCallback<AVUser>() {
                    @Override
                    public void done(List<AVUser> list, AVException e) {//在后台开始异步查询
                        if(e == null){

                            if(list!=null && list.size()>0){
                                //获取当前用户的所有好友
                                List<String> contacts = DBUtils.initContacts(currentUser);
                                //List<AVUser> list 查询到的满足条件的数据
                                addFriendView.onGetSearchResult(true,list,contacts,null);
                            }else{
                                //说明查询成功 但是没有满足查询条件的数据
                                addFriendView.onGetSearchResult(false,null,null,"没查到满足条件的好友");
                            }

                        }else{
                            addFriendView.onGetSearchResult(false,null,null,e.getMessage());
                        }
                    }
                });
    }

    @Override
    public void addFriend(final String username) {
        ThreadUtils.runOnSubThread(new Runnable() {
            @Override
            public void run() {
                try {
                    //通过contactManager 进行好友的添加
                    EMClient.getInstance().contactManager().addContact(username,"请求你为好友");
                    //如果没走异常 并不是说好友添加成功 而是添加好友的请求成功发送到了服务端
                    ThreadUtils.runOnMainThread(new Runnable() {
                        @Override
                        public void run() {
                            addFriendView.onGetAddFriendResult(true,null);
                        }
                    });
                } catch (final HyphenateException e) {
                    e.printStackTrace();
                    ThreadUtils.runOnMainThread(new Runnable() {
                        @Override
                        public void run() {
                            addFriendView.onGetAddFriendResult(false,e.getMessage());
                        }
                    });
                }
            }
        });

    }
}
